package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.*;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for (int i = 4; i > intCode.toString().length(); i--) {

            unitCode = "0" + unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    /**
     * 查询库存所有的商品信息
     *
     * @return
     */
    @Override
    public List<Goods> getAllgoods(Integer page, Integer rows) {
        List<Goods> list = new ArrayList<>();
        PageHelper.startPage(page, rows);
        list = goodsDao.getAllgoods();
        return list;
    }

    /**
     * 查询所有商品信息（可根据家具家电分类、名称查询）
     *
     * @param page        第几页
     * @param rows        每页的数据数
     * @param goodsName   商品名称
     * @param goodsTypeId 商品类型id
     * @return
     */
    @Override
    public List<Goods> getGoodsByType(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {

        PageHelper.startPage(page, rows);

        List<Goods> goodsList = goodsDao.getGoodsByType(goodsName, goodsTypeId);

        return goodsList;
    }

    /**
     * 商品添加或修改
     *
     * @param goods
     * @return
     */
    @Override
    public ServiceVO addGoods(Goods goods) {

        if (goods.getGoodsId() == null) {
            //商品编号
            goods.setGoodsCode(UUID.randomUUID().toString());
            //库存数量
            goods.setInventoryQuantity(goods.getMinNum());
            //上次采购价格
            goods.setLastPurchasingPrice(0);
            //商品状态
            goods.setState(0);
            goodsDao.addGoods(goods);
        } else {
            goodsDao.updateGoods(goods);
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
    }

    /**
     * 商品删除（要判断商品状态,入库、有进货和销售单据的不能删除）
     *
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO deleteGoods(Integer goodsId) {

        Goods goods = goodsDao.getGoodsById(goodsId);
        if (goods.getState() == 0) {
            goodsDao.deleteGoods(goodsId);
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
    }

    /**
     * 无库存商品列表展示（可以根据商品名称或编码查询）
     *
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public List<Goods> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        List<Goods> goodsList = goodsDao.getNoInventoryQuantity();
        PageHelper.startPage(page, rows);
        return goodsList;
    }

    /**
     * 有库存商品列表展示（可以根据商品名称或编码查询）
     *
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public List<Goods> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        List<Goods> goodsList = goodsDao.getHasInventoryQuantity();
        PageHelper.startPage(page, rows);
        return goodsList;
    }

    /**
     * 添加库存、修改数量或成本价
     *
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     * @return
     */
    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        Goods goods = goodsDao.getGoodsById(goodsId);
        goods.setInventoryQuantity(inventoryQuantity);
        goods.setPurchasingPrice(purchasingPrice);
        goodsDao.updateGoods(goods);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
    }

    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        Goods goods = goodsDao.getGoodsById(goodsId);
        if (goods.getState()==0){
            goods.setInventoryQuantity(0);
            goodsDao.updateGoods(goods);
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }else {
            return new ServiceVO<>(ErrorCode.ACCOUNT_EXIST_CODE,ErrorCode.REQ_ERROR_MESS);
        }

    }

    /**
     * 查询所有 当前库存量 小于 库存下限的商品信息
     * @return
     */
    @Override
    public List<Goods> listAlarm() {

        return goodsDao.listAlarm();
    }


}
